<template>
	<view class="MyAddress">
		<view class="MyAddress_add1" v-if="addressList.length == 0">
			<!-- <view>请添加地址</view> -->
			<wanl-empty  text="没有找到任何地址" src="address_default3x"></wanl-empty>
		</view>
		<!-- 显示地址 -->
		<view class="MyAddress_item" v-for="(item,index) in addressList" :key="index" @tap="checkAddress(item)">
			<view class="MyAddress_item_title">
				<view class="MyAddress_item_title_left">
					<view class="name">{{item.name}}</view>
					<view class="phone">{{item.mobile}}</view>
				</view>
				<view class="MyAddress_item_title_right" @tap.stop="getEditAddress('edit', item)">编辑</view>
			</view>
			<view class="MyAddress_item_add">{{ item.province }} {{ item.city }}  {{ item.district }} {{item.address}}</view>
			<view class="line"></view>
			<view class="MyAddress_item_edit">
				<view class="MyAddress_item_edit_left">
					<view class="MyAddress_item_edit_left_image" v-if="item.default==1">
						<image src="../../../static/image/order/xuanzhong.png"></image>
					</view>
					<view class="MyAddress_item_edit_left_image" v-if="item.default==0">
						<image src="../../../static/image/order/weixuanzhong.png"></image>
					</view>
					<view class="title">默认地址</view>
				</view>
				<view class="MyAddress_item_edit_right" @tap.stop="delAddress(item.id, index)">删除</view>
			</view>
		</view>
		
		<!-- 添加地址 -->
		<view class="MyAddress_add" @tap="addAddress('add')">+添加地址</view>
		
	</view>
</template>

<script>
	import wanlEmpty from '../../../components/wanl-empty/wanl-empty.vue'
	export default {
		data() {
			return {
				isShow:true,
				source: 0,
				order_id: 0,
				addressList: [],
				modalName: null,
				listTouchStart: 0,
				listTouchDirection: null,
				//上拉下拉
				total: 0, //数据量
				current_page: 1, //当前页码
				last_page: 1 ,//总页码
				status: 'loading',
				contentText: {
					contentdown: ' ',
					contentrefresh: '正在加载..',
					contentnomore: ''
				}
			};
		},
		components:{
			wanlEmpty
		},
		methods:{
			async loadData(reload, refresh) {
				this.$api.get({
					url: '/wanlshop/address/getaddress',
					loadingTip: refresh?'更新列表中...':'',
					data:{
						page: reload?(this.current_page):1
					},
					success: res => {
						if(reload){
							this.addressList = this.addressList.concat(res.data);
						}else{
							this.addressList = res.data;
							uni.stopPullDownRefresh();
						}
						this.total = res.total; //数据量
						this.current_page = res.current_page; //当前页码
						this.last_page = res.last_page; //总页码
						this.status = res.total == 0 ? 'noMore':'more';
					}
				});
			},
			//选择地址
			checkAddress(item) {
				console.log('item',item)
				if (this.source == 1) {
					console.log('item1',item)
					this.$wanlshop.prePage().loadData(item.id);
					this.$wanlshop.back(1);
				}else if(this.source == 2){ //我的订单修改地址  地址ID   返回订单ID
				console.log('item2',item)
					this.$wanlshop.prePage().refreshList(item.id, this.order_id);
					this.$wanlshop.back(1);
				}
			},
			// 新增地址
			addAddress(type, item) {
				this.$wanlshop.to(`../AddAddress/AddAddress?type=${type}&data=${JSON.stringify(item)}`);
			},
			// 点击编辑跳转到修改收获地址页面
			getEditAddress(type, item){			
					
			this.$wanlshop.to(`../editAddress/editAddress?type=${type}&data=${JSON.stringify(item)}`);
			},
			// 删除
			async delAddress(id, key) {
				this.$delete(this.addressList, key); //vue方法
				this.$api.post({
					url: '/wanlshop/address/deladdress',
					data: { id: id }
				});
			},
			async loadData(reload, refresh) {
				this.$api.get({
					url: '/wanlshop/address/getaddress',
					loadingTip: refresh?'更新列表中...':'',
					data:{
						page: reload?(this.current_page):1
					},
					success: res => {
						console.log()
						if(reload){
							this.addressList = this.addressList.concat(res.data);
						}else{
							this.addressList = res.data;
							uni.stopPullDownRefresh();
						}
						this.total = res.total; //数据量
						this.current_page = res.current_page; //当前页码
						this.last_page = res.last_page; //总页码
						this.status = res.total == 0 ? 'noMore':'more';
						console.log('addressList',this.addressList)
					}
				});
			},
			//添加或修改成功之后回调
			async refreshList(data, type) {
				this.$api.post({
					url: '/wanlshop/address/address',
					data: {data: data,type: type},
					success: (res) => {
						this.loadData(false,true);
					}
				});
			},
		},
		onLoad(option) {
			this.source = option.source;
			this.order_id = option.order_id;
			this.loadData()
		}
	}
</script>

<style lang="scss">
page {
		background-color: #f0f0f0;
	}
	.MyAddress{
		.MyAddress_add1{
			// background-color: pink;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #808080;
			height:1000rpx;
		}
		.MyAddress_item{
			background-color: #FFFFFF;
			width: 700rpx;
			// height: 300rpx;
			border-radius: 10rpx;
			margin: 20rpx auto;
			overflow: hidden;
			box-sizing: border-box;
			&_title{
				display: flex;
				// align-items: center;
				justify-content: space-between;
				// background-color: pink;
				// height: 80rpx;
				margin: 20rpx ;
				&_left{
					display: flex;
					align-items: center;
					// background-color: red;
					color:#282828;
					.name{
						font-size: 30rpx;						
						font-weight:600;
						margin-right: 30rpx;
					}
					.phone{
						font-size: 26rpx;
						
					}
				}
				&_right{
					color: #61abff;
					font-size: 26rpx;
					
				}
			}
			&_add{
				font-size: 26rpx;
				color: #282828;
				// background-color: pink;
				width: 600rpx;
				margin: 0 20rpx 20rpx;
			}
			.line{
				width: 750rpx;
				height: 1rpx;
				background-color: #F5F5F5;
				// background-color: red;
			}
			&_edit{
				// background-color: pink;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 20rpx;
				&_left{
					display: flex;
					align-items: center;
				
					&_image{
							// background-color: red;
							width: 30rpx;
							height: 30rpx;
							display: flex;
							align-items: center;
						image{
							width: 30rpx;
							height: 30rpx;
						}
					}
					.title{
						font-size: 26rpx;
						color:#282828;
						margin-left: 15rpx;
					}
				}
				&_right{
					font-size: 26rpx;
					color: #282828;
				}
			}
		}
		// 添加地址
		.MyAddress_add{
			position: fixed;
			left: 25rpx;
			bottom: 40rpx;
			background-color:#ff3200 ;
			color: #FFFFFF;
			font-size: 30rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			width: 700rpx;
			border-radius: 50rpx;
			
		}
		}
</style>
